#!/bin/sh
#
# pkgsrc-bootstrap
#
# Copyright 2007, 2008, 2009  Ole Andre Rodlie <olear@slackforge.net>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

CWD=$(pwd)
GET=wget
URL=http://ftp.netbsd.org/pub/pkgsrc
PKG=/tmp/package-pkgsrc-bootstrap
VERSION=2008Q3
ARCH=${ARCH:-i486}

if [ -f /etc/slackware-version ]; then
  BUILD=1_$(cat /etc/slackware-version | sed 's/\.//g;s/Slackware /slack/;s/ /_/g') 
else
  BUILD=1
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -pipe -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG || exit 1

if [ -d /usr/pkg ]; then
  ( cd /usr ; mv pkg pkg.$$ )
fi
if [ -f /etc/rc.conf ]; then
  ( cd /etc ; mv rc.conf rc.conf.$$ )
fi
if [ -f /etc/rc.subr ]; then
  ( cd /etc ; mv rc.subr rc.subr.$$ )
fi
if [ -d /usr/pkgsrc ]; then
  ( cd /usr ; mv pkgsrc pkgsrc.$$ )
fi

mkdir -p /usr/pkg || exit 1

if [ ! -f $CWD/pkgsrc-$VERSION.tar.bz2 ]; then
  ( cd $CWD ; $GET $URL/pkgsrc-$VERSION/pkgsrc-$VERSION.tar.bz2 )
fi

tar xvjf $CWD/pkgsrc-$VERSION.tar.bz2 -C /usr || exit 1

cd /usr/pkgsrc/bootstrap || exit 1
unset PKG_PATH
CFLAGS="$SLKCFLAGS" sh bootstrap --prefix=/usr/pkg --pkgdbdir=/usr/pkg/var/db/pkg --varbase=/usr/pkg/var || exit 1
( cd ../ ;
  ( cd pkgtools ;
    ( cd rc.subr ; /usr/pkg/bin/bmake install clean )
    ( cd pkg_chk ; /usr/pkg/bin/bmake install clean )
    ( cd pkg_tarup ; /usr/pkg/bin/bmake install clean )
  )
)

rm -rf /usr/pkgsrc/*/*/work

mkdir -p $PKG/usr
( cd /usr ; mv pkg $PKG/usr/ )
mkdir -p $PKG/usr/pkg/etc
mkdir -p $PKG/usr/pkg/var/log
mkdir -p $PKG/etc/profile.d
cat $CWD/pkgsrc.sh > $PKG/etc/profile.d/pkgsrc.sh || exit 1
chmod 755 $PKG/etc/profile.d/pkgsrc.sh || exit 1
mv /etc/rc.subr $PKG/etc/ || exit 1
mv /etc/rc.conf $PKG/etc/rc.conf.new || exit 1
rm /etc/rc.conf.example

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

rm -rf $PKG/usr/pkg/man/cat*
rm -f $PKG/usr/pkg/etc/mk.conf || exit 1
cat $CWD/mk.conf > $PKG/usr/pkg/etc/mk.conf.new || exit 1

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n ../pkgsrc-bootstrap-$VERSION-$ARCH-$BUILD.tgz
